package com.fatgirl.baseUI.visual
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import com.fatgirl.baseUI.IRadioButton;
	import com.fatgirl.baseUI.IRadioButtonGroup;
	
	/**
	 * 自定义 单选按钮可视化组 
	 * 默认为可视化编辑 无动态添加 单选按钮功能
	 * @author iceman
	 * 
	 */
	public class VRadioButtonGroup extends Sprite implements IRadioButtonGroup
	{
		private var m_name:String;
		private var m_selectedData:Object;
		private var m_IRadioButton:IRadioButton;
		private var m_numRadioButtons:int;
		private var m_RadioButtonList:Vector.<IRadioButton>;
		public function VRadioButtonGroup()
		{
			m_RadioButtonList = new Vector.<IRadioButton>;
			init();
		}
		public override function get name():String
		{
			return m_name;
		}
		
		public override function set name(_str:String):void
		{
			m_name = _str;
		}
		
		public function get numRadioButtons():int
		{
			return m_numRadioButtons;
		}
		
		public function get selectedData():Object
		{
			return m_selectedData;
		}
		
		public function set selectedData(_value:Object):void
		{
			m_selectedData = _value;
		}
		
		public function get selection():IRadioButton
		{
			return m_IRadioButton;
		}
		
		public function set selection(_IRadioButton:IRadioButton):void
		{
			if(_IRadioButton)
			{
				if(m_IRadioButton) m_IRadioButton.selected = false;
				m_IRadioButton = _IRadioButton;
				m_selectedData = m_IRadioButton.value;
				m_IRadioButton.selected = true;
			}
		}
		private function init():void
		{
			for(var i:int=0;i<this.numChildren;i++) 
			{
				if(getChildAt(i) is IRadioButton)
				{
					m_RadioButtonList.push(getChildAt(i));
					if(getChildAt(i) is Sprite) 
					{
						(getChildAt(i)as Sprite).addEventListener(MouseEvent.CLICK,clickHandler);
					}
				}
			}
			m_numRadioButtons = m_RadioButtonList.length;
		}
		private function clickHandler(e:MouseEvent):void
		{
			var t_IRadioButton:IRadioButton = (e.currentTarget as IRadioButton);
			this.selection = t_IRadioButton;
		}
	}
}